package gesconda.CBRKBRecommender.data;

import gesconda.CBRKBRecommender.model.CMMethodChar;
import gesconda.CBRKBRecommender.model.CMMethodTable;
import gesconda.CBRKBRecommender.model.CMTaulaMethodChar;
import gesconda.master.model.CMAtribut;
import gesconda.master.model.CMAtributContinu;
import gesconda.master.model.CMAtributDiscretNoOrdenat;
import gesconda.master.model.CMInstancia;
import gesconda.master.model.CMModel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class CSVImport {
	
	

	public static void readFile(CMMethodTable method_taula, File f) throws IOException {
		CMTaulaMethodChar taula = method_taula.taula;
		try{BufferedReader bufRdr = new BufferedReader(new FileReader(f));
		String reqLine, line, line0, line1;
		StringTokenizer reqSt, st, st2;

		reqLine = bufRdr.readLine();
		reqSt = new StringTokenizer(reqLine, ",");
		
		// Llegim la primera fila que cont� els noms dels atributs
		line0 = bufRdr.readLine();
		line1 = bufRdr.readLine();
		st = new StringTokenizer(line0, ",");
		st2 = new StringTokenizer(line1, ",");st2.countTokens();

		ArrayList valors = new ArrayList();
//		while (st.hasMoreTokens() && st2.hasMoreTokens()) {
		while (st.hasMoreTokens()) 
		{
			String nomAtribut = st.nextToken();
			if(nomAtribut == null)
				System.out.println("hsdhkjsahekhakehgasklhjks");
			
			String primerValor;
			if(st2.hasMoreTokens())
				primerValor = st2.nextToken();
			else 
				primerValor = "";
			if(primerValor == null)
				System.out.println("sakjdfaksjkasjgalksgdlsdglsdñ");
			
			String reqValor;
			if(reqSt.hasMoreTokens()) {
				reqValor = reqSt.nextToken();
				System.out.println("reqValor = " + reqValor);
			}
			else 
				reqValor = "";
			
			
			CMAtribut a;
			
			try {
				valors.add(Float.parseFloat(primerValor.replace(',', '.')));
				a = taula.addAtribut(new CMAtributContinu(nomAtribut), null);
			} catch (NumberFormatException e) {
				// no es valor numeric -> Atribut DiscretNoOrdenat
				valors.add(primerValor);
				a = taula.addAtribut(new CMAtributDiscretNoOrdenat(nomAtribut), null);
			}
			if(reqValor == null)
				System.out.println("req_sakjdfaksjkasjgalksgdlsdglsdñ");
			else {
				CMMethodChar method_car = new CMMethodChar(a, reqValor);
				method_taula.mRequirements.add(method_car);
			}
		}
		int contador = 1;
		// insertar la primera instancia llegida
		CMInstancia instancia = new CMInstancia(Integer.toString(contador++));
		for (int i = 0; i < valors.size(); i++) {
			instancia.afegirValorAtribut(taula.getAtribut(i), valors.get(i).toString());
		}
		taula.afegeixInstancia(instancia);

		// Llegim la resta de linies fins a final del fitxer
		while ((line = bufRdr.readLine()) != null) {
			instancia = new CMInstancia(Integer.toString(contador++));
			st = new StringTokenizer(line, ";");
			int i = 0;
			while (st.hasMoreTokens()) {
				String valor = st.nextToken().replace(',', '.');
				instancia.afegirValorAtribut(taula.getAtribut(i++), valor);
			}
			taula.afegeixInstancia(instancia);
		}
		bufRdr.close();
		}
		catch(IOException e)
		{
			System.err.println("kjsafkjaslkdfjalsd");
		}
	}

}
